
package com.android.formula.utils;

import android.text.TextUtils;
import android.util.Log;

import com.android.formula.common.MyApplication;

/**
 * Define method used to log message for debugging: log error, log debug, log
 * info.
 */
public class LogUtils {

    /**
     * Log info.
     *
     * @param tag tag name
     * @param msg message wants to log
     */
    public static void logError(final String tag, final String msg) {
        if (MyApplication.isBuildDebug() && !TextUtils.isEmpty(msg)) {
            Log.e(tag, msg);
        }
    }

    /**
     * Log debug.
     *
     * @param tag tag name
     * @param msg message wants to log
     */
    public static void logDebug(final String tag, final String msg) {
        if (MyApplication.isBuildDebug() && !TextUtils.isEmpty(msg)) {
            Log.d(tag, msg);
        }
    }

    /**
     * Log info.
     *
     * @param tag tag name
     * @param msg message wants to log
     */
    public static void logInfo(final String tag, final String msg) {
        if (MyApplication.isBuildDebug() && !TextUtils.isEmpty(msg)) {
            Log.i(tag, msg);
        }
    }

    /**
     * Log info: What the fuck!!!
     */
    public static void wtf() {
        if (MyApplication.isBuildDebug()) {
            Log.i("WTF", "What the fuck!!!");
        }
    }

    /**
     * ******************************************* Baas
     * *************************************************************
     */

    public static StackTraceElement getStackTraceElement(int hierarchy) {
        StackTraceElement[] stack = Thread.currentThread().getStackTrace();
        StackTraceElement info = new StackTraceElement("", "", "", 0);
        // StackTraceElementとしては
        // [0] class:dalvik.system.VMStack , method:getThreadStackTrace
        // [1] class:java.lang.Thread , method:getStackTrace
        // [2] class:LogUtil , method:getStackTraceElement
        // [3]　呼び元
        // がとれるので先頭のindex:3が呼び元
        int calledFuncIndex = 3 + hierarchy;
        if ((stack != null) && (stack.length > calledFuncIndex)) {
            info = stack[calledFuncIndex];
        }
        return info;
    }

    static public void logStartFunc() {
        StackTraceElement element = getStackTraceElement(1);
        String clz = getSimpleClassName(element.getClassName());
        String method = element.getMethodName();
        LogUtils.d(clz, "[START]:" + method);
    }

    // static public void logEndFunc() {
    // StackTraceElement element = getStackTraceElement(1);
    // String clz = getSimpleClassName(element.getClassName());
    // String method = element.getMethodName();
    // LogUtils.d(clz, "[END]:" + method);
    // }

    // static public String getCurrentClassName() {
    // StackTraceElement element = getStackTraceElement(1);
    // String clz = getSimpleClassName(element.getClassName());
    // return clz;
    // }

    // static public String getCurrentMethodName() {
    // StackTraceElement element = getStackTraceElement(1);
    // String method = element.getMethodName();
    // return method;
    // }

    private static String getSimpleClassName(String name) {
        String simpleName = name;
        if (name != null) {
            int n = name.lastIndexOf('.');
            if (n > -1) {
                simpleName = name.substring(n + 1);
            }
        }
        return simpleName;
    }

    /**
     * @see android.util.Log#e(String, String)
     */
    static public void e(String tag, String msg) {
        Log.e(tag, msg);
    }

    /**
     * @see android.util.Log#w(String, String)
     */
    static public void w(String tag, String msg) {
        if (!MyApplication.isBuildDebug()) {
            return;
        }
        Log.w(tag, msg);
    }

    /**
     * @see android.util.Log#i(String, String)
     */
    static public void i(String tag, String msg) {
        if (!MyApplication.isBuildDebug()) {
            return;
        }
        Log.i(tag, msg);
    }

    /**
     * @see android.util.Log#d(String, String)
     */
    static public void d(String tag, String msg) {
        if (!MyApplication.isBuildDebug()) {
            return;
        }
        Log.d(tag, msg);
    }

}
